// JSPAI下单返回的prepay_id 预支付交易会话标识。用于后续接口调用中使用，该值有效期为2小时
// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml
import {
  trim, compact, get, isEmpty
} from 'lodash';
import { DataTypes, Model, UUIDV4 } from 'sequelize';
import { MySqlModel, getSequelize } from '../db';
import { WxPrepayPaidStatus, getToday } from '../utils';

class ReservationPrepay extends MySqlModel {

}

ReservationPrepay.init({
  id: {
    type: DataTypes.UUID, unique: 'id', allowNull: false, defaultValue: UUIDV4, validate: { isUUID: 4 }, primaryKey: true, comment: '主键'
  },
  hotel_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '主体'
  },
  openid: {
    type: DataTypes.STRING(50), allowNull: false, comment: '用户唯一标识'
  }, // 发起人
  appid: {
    type: DataTypes.STRING(50), allowNull: false, comment: '公众号的唯一标识'
  },
  mchid: {
    type: DataTypes.STRING(50), allowNull: true, defaultValue: null, comment: '直连商户号'
  },
  business_date: {
    type: DataTypes.DATEONLY, allowNull: false, comment: '交易日期'
  },
  confirm_number: {
    type: DataTypes.STRING(50), allowNull: false, comment: '确认号'
  },
  resv_name_id: {
    type: DataTypes.STRING(50), allowNull: false, comment: 'resv_name_id'
  },
  primary_confirm_number: {
    type: DataTypes.STRING(50), allowNull: false, comment: '主预订确认号'
  },
  primary_resv_name_id: {
    type: DataTypes.STRING(50), allowNull: false, comment: '主预订resv_name_id'
  },
  out_trade_no: {
    type: DataTypes.STRING(50), unique: 'out_trade_no', allowNull: false, comment: '商户订单号'
  },
  wx_transaction_id: {
    type: DataTypes.STRING(50), allowNull: true, defaultValue: null, comment: '微信支付系统生成的订单号'
  },
  prepay_id: {
    type: DataTypes.STRING(120), allowNull: true, defaultValue: null, comment: '预支付交易会话标识'
  },
  expires_at: {
    type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: 'prepay_id失效时间'
  },
  amount: {
    type: DataTypes.DECIMAL(10, 2), allowNull: false, defaultValue: 0, comment: '订单待支付金额'
  },
  refund_amount: {
    type: DataTypes.DECIMAL(10, 2), allowNull: false, defaultValue: 0, comment: '退款金额'
  },
  paid_status: { // Waiting 待支付，Terminated 终止支付（失效），Success 支付成功
    type: DataTypes.STRING(20), allowNull: false, comment: '交易状态'
  },
  paid_time: { // 支付时间
    type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: '支付时间'
  },
  share_deposit_rule: { // Integration合并支付 or Going Dutch各付各的
    type: DataTypes.STRING(20), allowNull: false, comment: '合住预订押金支付规则'
  },
  is_pms_posted: {
    type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, comment: 'PMS是否入账成功'
  },
  is_refund: { // 退款明细另存一个表.
    type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true, comment: '是否有退款'
  },
  created_at: {
    type: DataTypes.DATE, allowNull: false, comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE, allowNull: false, comment: '更新时间'
  },
  version: {
    type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: '版本控制'
  }
}, {
  sequelize: getSequelize(),
  tableName: 'reservation_prepay',
  comment: '预订微信支付下单表',
  indexes: [
    { fields: ['hotel_id', 'primary_confirm_number'] },
    { fields: ['wx_transaction_id'] },
  ]
});

ReservationPrepay.beforeValidate((instance, options) => {
  if (instance.isNewRecord) {
    instance.business_date = getToday();
  }
})

// https://sequelize.org/api/v6/class/src/model.js~model#static-method-hasMany

export default ReservationPrepay;
